What is string-left-right?
The string-left-right npm package provides utilities for trimming whitespace from the left and right sides of strings, as well as other string manipulation functionalities.
What are string-left-right's main functionalities?
trimLeft
The trimLeft function removes whitespace from the left side of a string.
const { trimLeft } = require('string-left-right');
const result = trimLeft(' Hello World! ');
console.log(result); // 'Hello World! '
trimRight
The trimRight function removes whitespace from the right side of a string.
const { trimRight } = require('string-left-right');
const result = trimRight(' Hello World! ');
console.log(result); // ' Hello World!'
trim
The trim function removes whitespace from both sides of a string.
const { trim } = require('string-left-right');
const result = trim(' Hello World! ');
console.log(result); // 'Hello World!'
left
The left function returns the index of the first non-whitespace character from the left.
const { left } = require('string-left-right');
const result = left(' Hello World! ');
console.log(result); // 3
right
The right function returns the index of the first non-whitespace character from the right.
const { right } = require('string-left-right');
const result = right(' Hello World! ');
console.log(result); // 15
Other packages similar to string-left-right
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and strings. It includes functions like _.trim, _.trimStart, and _.trimEnd which are similar to the trim, trimLeft, and trimRight functions in string-left-right.
underscore
Underscore is another utility library similar to Lodash. It provides a variety of functions for working with arrays, objects, and strings. The _.trim function in Underscore is comparable to the trim function in string-left-right.
validator
Validator is a library for string validation and sanitization. It includes functions like trim, ltrim, and rtrim which are similar to the trim, trimLeft, and trimRight functions in string-left-right. However, Validator focuses more on validation and sanitization rather than general string manipulation.
string-left-right
Looks up the first non-whitespace character to the left/right of a given index
Install
This package is pure ESM. If you're not ready yet, install an older version of this program, 4.1.0 (npm i string-left-right@4.1.0
).
npm i string-left-right
Quick Take
import { strict as assert } from "assert";
import {
left,
right,
leftSeq,
rightSeq,
chompLeft,
chompRight,
leftStopAtNewLines,
rightStopAtNewLines,
} from "string-left-right";
const str = "abc def";
assert.equal(
`next non-whitespace character to the left of ${str[6]} (index 6) is ${
str[left(str, 6)]
} (index ${left(str, 6)})`,
"next non-whitespace character to the left of d (index 6) is c (index 2)",
);
Documentation
Please visit codsen.com for a full description of the API.
Contributing
To report bugs or request features or assistance, raise an issue on GitHub.
Licence
MIT License.
Copyright © 2010-2024 Roy Revelt and other contributors.